<template>
  <div class="record mx-5 my-5">
    <div>
      <div style="margin-bottom: 10px;display: flex;align-items: center">
        <el-date-picker
                style="width: 30%;"
                v-model="value"
                prefix-icon="el-icon-date"
                type="daterange"
                size="medium"
                :shortcuts="shortcuts"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
        ></el-date-picker>
        <el-input
                placeholder="请输入关键字"
                v-model="params.keyword"
                clearable
                size="medium"
                style="width: 20%;margin-left: 20px"
        ></el-input>
        <el-button size="medium" style="margin-left: 20px" @click="toTime" type="primary">确定</el-button>
        <el-button size="medium" style="margin-left: 20px" @click="reBack">重置</el-button>
      </div>
      <div style="display: flex;align-items: center" v-if="currentLeve > 0">
        <el-button size="medium" style="margin: 10px" @click="toBackLeve">返回</el-button>
        <div style="flex: 1;text-align: center;padding-right: 10px;font-size: 20px;font-weight: bold">{{levePromiseList[currentLeve-1].nickname || ''}}的下级</div>
      </div>
      <div v-if="currentLeve == 0" style="font-size: 20px;font-weight: bold;margin: 10px 0">
        下级数量：{{count}}
      </div>
      <el-table
        :data="list"
        :empty-text="loading.list ? '数据加载中' : '暂无数据'"
        cell-class-name="custom-row"
        header-row-class-name="custom-row"
        header-cell-class-name="custom-header"
      >
        <el-table-column label="我的下级">
          <template #default="scope">
            {{ scope.row.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="奖励总额">
          <template #default="scope"> {{ scope.row.rebate_total }}</template>
        </el-table-column>
        <el-table-column label="时间">
          <template #default="scope">
            {{
            $dayjs.unix(scope.row.created_at).format("YYYY-MM-DD HH:mm:ss")
            }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="medium" @click="checkLeve(scope.row)" v-if="currentLeve <= 1">查看下级</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="d-flex my-5 justify-center">
        <el-pagination
          :page-size="10"
          :total="total"
          background
          hide-on-single-page
          layout="prev, pager, next, total"
          @current-change="currentChange"
          v-model:current-page="params.page"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
  import {computed, defineComponent, onMounted, reactive, toRaw, toRefs, watch} from "vue";
import {PromoteService} from "@/services/PromoteService";
import Empty from "@/components/empty/Index.vue";
  import {UserBattleRole} from "@/views/battle/services";
  import {useStore} from "@/store";

export default defineComponent({
  name: "RechargeRecord",
  components: { Empty },
  props: {},
  setup() {
    const store = useStore();
    const shortcuts = [
      {
        text: "最近一周",
        value: (() => {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          return [start, end];
        })(),
      },
      {
        text: "最近一个月",
        value: (() => {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          return [start, end];
        })(),
      },
      {
        text: "最近三个月",
        value: (() => {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          return [start, end];
        })(),
      },
    ];
    const state = reactive({
      list: [],
      total: null,
      loading: {
        list: false,
      },
      params:{
        page: 1,
        page_size: 10,
        start_time:'',
        end_time:'',
        keyword:''
      },
      data:{
        start_time:'',
        end_time:'',
      },
      value:[],
      userInfo: computed(() => store.getters.userInfo),
      levePromiseList:[{},{}],
      currentLeve:0,
      count:0
    });
    watch(() => state.userInfo, (newVal) => {
      if(newVal && newVal.role == 'anchor'){
        getRechargeRecord();
      }
    })
    onMounted(() => {
      getRechargeRecord();
    });
   function getMyLeveNum() {
     PromoteService.getUserInviterCount().then(res =>{
       if(res.data.code == 0){
         state.count = res.data.data.count
       }
     })
   }
    function toBackLeve() {
      state.currentLeve = state.currentLeve -1
      state.params.page = 1
      state.params.start_time = ''
      state.params.end_time = ''
      if(state.currentLeve == 0){
        state.params.inviter_id = ''
      }else if(state.currentLeve == 1){
        state.params.inviter_id = state.levePromiseList[0].invitees_id
      }else {
        state.params.inviter_id = state.levePromiseList[1].invitees_id
      }
      getRechargeRecord()
    }

    function checkLeve(row) {
      state.currentLeve = state.currentLeve + 1
      state.levePromiseList[state.currentLeve - 1] = row
      state.params.page = 1
      state.params.start_time = ''
      state.params.end_time = ''
      state.params.inviter_id = row.invitees_id
      getRechargeRecord()
    }
    function getTimeStamp(dateStr) {
      let timestamp = Date.parse(dateStr);
      return timestamp / 1000;
    }

    function toTime() {
      let arr = toRaw(state.value)
      if(arr.length > 1){
        state.params.start_time = getTimeStamp(arr[0])
        state.params.end_time = getTimeStamp(arr[1])
      }
      state.params.page = 1
      getRechargeRecord()
    }
    function reBack() {
      state.params.start_time = ''
      state.params.end_time = ''
      state.params.keyword = ''
      state.value = []
      state.params.page = 1
      getRechargeRecord()
    }
    const getRechargeRecord = async () => {
      if(state.userInfo && state.userInfo.role == 'anchor'){
        getMyLeveNum()
        state.loading.list = true;
        const res = await PromoteService.record(state.params).finally(() => {
          state.loading.list = false;
        });
        state.list = res.data.data.list;
        state.total = res.data.data.total;
      }
    };

    const currentChange = async (val) => {
      state.params.page = val
      getRechargeRecord()
    };

    return {
      ...toRefs(state),
      currentChange,
      shortcuts,
      toTime,
      reBack,
      checkLeve,
      toBackLeve
    };
  },
});
</script>

<style lang="scss" scoped>
.recharge-record {
  width: 100%;
  position: relative;
  padding: 0 18px;
  box-sizing: border-box;
  min-height: 100%;

  .table {
    width: 98%;
    margin: 0 auto;

    .head {
      display: flex;
      justify-content: space-around;
      align-items: center;
      height: 60px;
      text-align: center;

      > div {
        color: #989898;
        font-size: 1.17em;
        font-weight: bold;
      }
    }

    .body {
      text-align: center;

      .row {
        height: 70px;
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1rem;
        font-weight: 700;
        margin-bottom: 4px;

        .col {
          height: 68px;
          line-height: 68px;
          min-width: 25%;
          background-color: #2c2c32;
          margin-left: 0.5rem;

          &:first-child {
            margin-left: 0;
          }
        }
      }
    }
  }

  .recharge-type {
    width: 100%;
    height: 80px;
    margin: 0 auto;
    display: flex;
    border-bottom: 1px solid #4c4c53;

    > div {
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      width: 10rem;
      height: 100%;
      font-weight: 900;
      border-bottom: 3px solid transparent;
      color: #c0c0c2;
      margin-left: 30px;
    }

    .current {
      border-bottom-color: #7668cb;
      color: #7668cb;
    }
  }
}
</style>
